<?php
/**
 * Created by PhpStorm.
 * User: RenBin
 * Date: 2016/3/10
 * Time: 11:33
 */

namespace common\models\Service;


use common\models\DAO\Active;
use common\models\DAO\ActiveList;
use common\models\DAO\Exhibit;
use common\models\DAO\UserCoupon;
use common\models\DAO\UserRate;
use common\msg\UserMsg;

class ActService {

    public static function init($id, $userID){
        $actionInfo = Active::findOne($id);
        if(!$actionInfo)
            return ['status' => 0];
        $now = time();
        $result = ['status'=>1, 'info'=>$actionInfo->attributes];
        if($now >= $actionInfo['endTime']){
            $result['status'] = -1;
            return $result;
        }
        $today = strtotime(date('Ymd'));
        $isGet = ActiveList::find()->where(['activeID'=>$id, 'userID'=>$userID])->andWhere("createTime>=$today")->count();
        $isGet > 0 && $result['status'] = 2;
        return $result;
    }

    public static function complete($param){
        $userID = $param['userID'];
        $id = $param['id'];
        $today = strtotime(date('Ymd'));
        $result['status'] = 0;
        $result['msg'] = UserMsg::$time_out;
        $actionInfo = Active::findOne($id);
        if(!$actionInfo)
            return $result;
        $now = time();
        if($now >= $actionInfo['endTime']){
            $result['msg'] = UserMsg::$action_end;
            return $result;
        }
        if($now < $actionInfo['beginTime']){
            $result['msg'] = UserMsg::$action_no;
            return $result;
        }
        $get_count = ActiveList::find()->where(['activeID'=>$id])->andWhere("createTime>=$today")->count();
        if($actionInfo['count'] != 0 && $get_count >= $actionInfo['count']){
            $result['msg'] = UserMsg::$action_over;
            return $result;
        }
        if(ActiveList::find()->where(['activeID'=>$id, 'userID'=>$userID])->andWhere("createTime>=$today")->count() > 0){
            $result['msg'] = UserMsg::$alread_hongbao;
            return $result;
        }

        $transaction = \Yii::$app->db->beginTransaction();
        if($actionInfo['typeID'] == 1){
            $userRate = new UserRate();
            $userRate->userID = $userID;
            $userRate->rate = $actionInfo['amount'];
            $userRate->createTime = $now;
            $rate_day = $actionInfo['day'];
            $lose_day = $rate_day + 1;
            $userRate->loseTime = date('Ymd', strtotime("+$lose_day day"));
            $userRate->day = $rate_day;
            $userRate->composeID = $id;
            $userRate->typeID = 2;
            $rate_result = $userRate->save();
            if(!$rate_result){
                $transaction->rollBack();
                return $result;
            }
            $gmID = $userRate->id;
        }else if($actionInfo['typeID'] == 2){
            $user_coupon = new UserCoupon();
            $user_coupon->userID = $userID;
            $user_coupon->createTime = $now;
            $user_coupon->card = $actionInfo['amount'];
            $lose_day = intval($actionInfo['day']) + 1;
            $user_coupon->loseTime = date('Ymd', strtotime("+$lose_day day"));
            $user_coupon->couponID = $id;
            $user_coupon->typeID = 2;
            $user_coupon_result = $user_coupon->save();
            if(!$user_coupon_result){
                $transaction->rollBack();
                return $result;
            }
            $gmID = $user_coupon->id;
        }else{
            $hb_param['userID'] = $userID;
            $hb_param['hongbao'] = $actionInfo['amount'];
            $hb_param['actID'] = $id;
            $hb_param['openID'] = $param['openID'];
            $hb_param['desc'] = $actionInfo['title'];
            $hb_result = HongBaoService::get_hb($hb_param);
            if(!$hb_result){
                $transaction->rollBack();
                return $result;
            }
            $gmID = $hb_result;
        }
        $activeList = new ActiveList();
        $activeList->userID = $userID;
        $activeList->activeID = $id;
        $activeList->createTime = $now;
        $activeList->typeID = $actionInfo['typeID'];
        $activeList->amount = $actionInfo['amount'];
        $activeList->day = $actionInfo['day'];
        $activeList->gmID = $gmID;
        if(!$activeList->save()){
            $transaction->rollBack();
            return $result;
        }
        $transaction->commit();
        return ['status'=>1];
    }

    public static function getSpread($id){
        $actionInfo = Exhibit::findOne($id);
        if(!$actionInfo)
            return false;
        return $actionInfo->attributes;

    }
}